
import { _decorator, Vec2} from 'cc';
import { ColorMem } from './colorMem';
const { ccclass,property } = _decorator;

@ccclass('BoardData')
export class BoardData {
    //棋盘，，棋盘上有6中可能，0迷雾没落子，1显示没落子，2迷雾笼罩黑棋，4显示的黑棋，8迷雾笼罩白棋，16显示的白棋
    boardArr: number[];
    //棋盘大小，是 n * n,n 只能是 9，13，19
    n: number;
    constructor(n: number) {
        this.n = n;
        this.boardArr = new Array<number>(n * n);
    }

    //获取棋子
    getBoradNum(v: Vec2): number {
        return this.boardArr[v.x * this.n + v.y];
    }

    //设置棋子
    setBoradNum(v2: Vec2, value: number): void {
        this.boardArr[v2.x * this.n + v2.y] = value;
    }

    static getArrNum(arr: number[], n: number, v: Vec2): number {
        return arr[v.x * n + v.y];
    }
    
    static setArrNum(arr: number[], n: number, v2: Vec2, value: number) {
        arr[v2.x * n + v2.y] = value;
    }

    //该点有子
    hasStones(v: Vec2): boolean {
        return (this.getBoradNum(v) & ColorMem.C_BLACK | this.getBoradNum(v) & ColorMem.C_WHITE) > 0;
    }

    //是否越界
    isBounds(v: Vec2): boolean {
        return v.x < 0 || v.y < 0 || v.x >= this.n || v.y >= this.n;
    }

    static yds: Vec2[] = new Array<Vec2>(new Vec2(1,0), new Vec2(0,1),new Vec2(-1,0),new Vec2(0,-1));

    //有气，落子周围没有其他棋子
    hasLiberty(v: Vec2): boolean {
        for (const yd of BoardData.yds) {
            if (!this.isBounds(yd.add(v)) && this.isEmpty(yd.add(v))) {
                return true;
            }
        }
        return false;
    }

    //该点上是空的，没有棋子
    isEmpty(v: Vec2): boolean {
        return (this.getBoradNum(v) | ColorMem.C_EMPTY) == ColorMem.C_EMPTY;
    }


}